package com.youlai.boot.factory.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.factory.mapper.SysTenantDatasourceMapper;
import com.youlai.boot.factory.service.SysTenantDatasourceService;
import com.youlai.boot.factory.model.entity.SysTenantDatasource;
import com.youlai.boot.factory.model.form.SysTenantDatasourceForm;
import com.youlai.boot.factory.model.query.SysTenantDatasourceQuery;
import com.youlai.boot.factory.model.vo.SysTenantDatasourceVO;
import com.youlai.boot.factory.converter.SysTenantDatasourceConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 租户数据源配置服务实现类
 *
 * @author youlaitech
 * @since 2025-11-22 08:31
 */
@Service
@RequiredArgsConstructor
public class SysTenantDatasourceServiceImpl extends ServiceImpl<SysTenantDatasourceMapper, SysTenantDatasource> implements SysTenantDatasourceService {

    private final SysTenantDatasourceConverter sysTenantDatasourceConverter;

    /**
    * 获取租户数据源配置分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysTenantDatasourceVO>} 租户数据源配置分页列表
    */
    @Override
    public IPage<SysTenantDatasourceVO> getSysTenantDatasourcePage(SysTenantDatasourceQuery queryParams) {
        Page<SysTenantDatasourceVO> pageVO = this.baseMapper.getSysTenantDatasourcePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取租户数据源配置表单数据
     *
     * @param id 租户数据源配置ID
     * @return 租户数据源配置表单数据
     */
    @Override
    public SysTenantDatasourceForm getSysTenantDatasourceFormData(Long id) {
        SysTenantDatasource entity = this.getById(id);
        return sysTenantDatasourceConverter.toForm(entity);
    }
    
    /**
     * 新增租户数据源配置
     *
     * @param formData 租户数据源配置表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSysTenantDatasource(SysTenantDatasourceForm formData) {
        SysTenantDatasource entity = sysTenantDatasourceConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新租户数据源配置
     *
     * @param id   租户数据源配置ID
     * @param formData 租户数据源配置表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSysTenantDatasource(Long id,SysTenantDatasourceForm formData) {
        SysTenantDatasource entity = sysTenantDatasourceConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除租户数据源配置
     *
     * @param ids 租户数据源配置ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSysTenantDatasources(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的租户数据源配置数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
